package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.yangzihui.CodeBo;
import com.cskaoyan.bean.yangzihui.CouponListPo;
import com.cskaoyan.bean.yangzihui.CouponMyListPo;
import com.cskaoyan.bean.yangzihui.IdBo;
import com.cskaoyan.service.CouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangzihui
 * @Description
 * @Create 2022-06-30 10:50
 * @Version
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    CouponService couponService;

    /**
     * 小程序首页进入优惠券专区
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit) {
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        CouponListPo couponListPo = couponService.list(page, limit, id);
        return BaseRespVo.ok(couponListPo);
    }

    /**
     * 从个人中心-优惠券进入我的优惠券页面
     *
     * @param status
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("mylist")
    public BaseRespVo myList(Integer status, Integer page, Integer limit) {
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        CouponMyListPo couponMyListPo = couponService.myList(id, status, page, limit);
        return BaseRespVo.ok(couponMyListPo);
    }

    /**
     * 领取优惠券功能
     *
     * @param idBo
     * @return
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody IdBo idBo) {
        if (!SecurityUtils.getSubject().isAuthenticated()) {
            return BaseRespVo.invalidParameter("请登录后领取");
        }
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        Integer couponId = idBo.getCouponId();
        Integer num = couponService.receive(id, couponId);
        if (num == -1) {
            return BaseRespVo.invalidData("剩余优惠券不足");
        } else if (num == 1) {
            return BaseRespVo.invalidData("领取数已达上限");
        } else if (num == 2) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("该优惠券限领数量错误");
    }

    /**
     * 兑换码兑换优惠券
     *
     * @param codeBo
     * @return
     */
    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody CodeBo codeBo) {
        // 获取当前登录的用户信息（id）
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        String code = codeBo.getCode();
        Integer num = couponService.exchange(id, code);
        if (num == 1) {
            return BaseRespVo.invalidData("领取数已达上限");
        } else if (num == 2) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData("没有该优惠券");
    }

    /**
     * 下单页面的优惠券选择
     *
     * @param cartId
     * @param grouponRulesId
     * @return
     */
    @RequestMapping("selectlist")
    public BaseRespVo selectList(Integer cartId, Integer grouponRulesId) {
        // 获取当前登录的用户信息（id）
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        CouponMyListPo couponMyListPo = couponService.selectlist(id, cartId, grouponRulesId);
        return BaseRespVo.ok(couponMyListPo);
    }
}
